using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using WIT.DAQ.Core.Models;
using WIT.DAQ.Core.Properties;

namespace WIT.DAQ.Core.Forms;

/// <summary>
///
/// </summary>
internal class StationMgrForm : BaseForm
{
	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private ContextMenuStrip cmsStation;

	private ToolStripMenuItem tsmiDel;

	private ToolStripMenuItem tsmiEdit;

	private Button btnAdd;

	private TreeView tvStations;

	private Button btnDel;

	private ToolStripMenuItem tsmiDAQSetting;

	private ToolStripSeparator toolStripSeparator1;

	private Button btnUp;

	private Button btnDown;

	private ToolStripMenuItem tsmiMonitor;

	/// <summary>
	///
	/// </summary>
	internal StationMgrForm()
	{
		InitializeComponent();
	}

	private void StationForm_Load(object sender, EventArgs e)
	{
		ToolTip toolTip = new ToolTip();
		toolTip.AutoPopDelay = 3000;
		toolTip.InitialDelay = 500;
		toolTip.ReshowDelay = 500;
		toolTip.ShowAlways = true;
		toolTip.SetToolTip(btnAdd, "添加站点");
		ToolTip toolTip2 = new ToolTip();
		toolTip2.AutoPopDelay = 3000;
		toolTip2.InitialDelay = 500;
		toolTip2.ReshowDelay = 500;
		toolTip2.ShowAlways = true;
		toolTip2.SetToolTip(btnDel, "删除站点");
		ToolTip toolTip3 = new ToolTip();
		toolTip3.AutoPopDelay = 3000;
		toolTip3.InitialDelay = 500;
		toolTip3.ReshowDelay = 500;
		toolTip3.ShowAlways = true;
		toolTip3.SetToolTip(btnUp, "上移");
		ToolTip toolTip4 = new ToolTip();
		toolTip4.AutoPopDelay = 3000;
		toolTip4.InitialDelay = 500;
		toolTip4.ReshowDelay = 500;
		toolTip4.ShowAlways = true;
		toolTip4.SetToolTip(btnDown, "下移");
		InitStations();
		tvStations.HideSelection = false;
		tvStations.DrawMode = TreeViewDrawMode.OwnerDrawText;
		tvStations.DrawNode += TvStations_DrawNode;
	}

	private void StationMgrForm_Shown(object sender, EventArgs e)
	{
		tvStations.SelectedNode = null;
	}

	/// <summary>
	/// 加载站点列表
	/// </summary>
	private void InitStations()
	{
		tvStations.Nodes.Clear();
		List<Station> allStations = Common.GetAllStations();
		foreach (Station item in allStations)
		{
			TreeNode node = new TreeNode
			{
				Name = item.Id.ToString(),
				Text = item.Name,
				ToolTipText = item.Desc,
				Tag = item
			};
			tvStations.Nodes.Add(node);
		}
	}

	/// <summary>
	/// treeview的选中节点在丢失焦点后仍然保持高亮
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void TvStations_DrawNode(object sender, DrawTreeNodeEventArgs e)
	{
		e.DrawDefault = true;
	}

	/// <summary>
	/// 新增站点
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnAdd_Click(object sender, EventArgs e)
	{
		StationEditForm stationEditForm = new StationEditForm(EnumFormPurpose.Add)
		{
			Icon = base.Icon,
			StartPosition = FormStartPosition.CenterParent
		};
		stationEditForm.SaveStation += StationEditForm_AddStation;
		stationEditForm.ShowDialog();
	}

	/// <summary>
	/// 新增站点-保存
	/// </summary>
	/// <param name="station"></param>
	private void StationEditForm_AddStation(Station station)
	{
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(Common.CONFIG_FILE_FULL_PATH);
		XmlElement documentElement = xmlDocument.DocumentElement;
		XmlElement xmlElement = (XmlElement)documentElement.SelectSingleNode("Stations");
		XmlElement xmlElement2 = xmlDocument.CreateElement("Station");
		xmlElement2.SetAttribute("Name", station.Name);
		xmlElement2.SetAttribute("Id", station.Id.ToString());
		xmlElement.AppendChild(xmlElement2);
		XmlElement xmlElement3 = xmlDocument.CreateElement("Desc");
		xmlElement3.InnerText = station.Desc;
		xmlElement2.AppendChild(xmlElement3);
		XmlElement xmlElement4 = xmlDocument.CreateElement("DefaultDeviceId");
		xmlElement4.InnerText = station.DefaultDeviceId.ToString();
		xmlElement2.AppendChild(xmlElement4);
		xmlDocument.Save(Common.CONFIG_FILE_FULL_PATH);
		TreeNode treeNode = new TreeNode
		{
			Name = station.Id.ToString(),
			Text = station.Name,
			ToolTipText = station.Desc,
			Tag = station
		};
		tvStations.Nodes.Add(treeNode);
		tvStations.SelectedNode = treeNode;
	}

	/// <summary>
	/// 删除站点
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnDel_Click(object sender, EventArgs e)
	{
		DeleteStation();
	}

	/// <summary>
	/// 删除站点
	/// </summary>
	private void DeleteStation()
	{
		if (tvStations.SelectedNode == null)
		{
			MessageBox.Show("请选择一个站点", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			return;
		}
		Station station = (Station)tvStations.SelectedNode.Tag;
		if (MessageBox.Show("确认删除此站点[" + station.Name + "]？\r所有相关设置都将随之失效，请谨慎操作。", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
		{
			return;
		}
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(Common.CONFIG_FILE_FULL_PATH);
		XmlElement documentElement = xmlDocument.DocumentElement;
		XmlElement xmlElement = (XmlElement)documentElement.SelectSingleNode("Stations");
		bool flag = false;
		foreach (XmlNode item in xmlElement.SelectNodes("Station"))
		{
			Guid guid = new Guid(item.Attributes["Id"].Value);
			if (guid == station.Id)
			{
				flag = true;
				xmlElement.RemoveChild(item);
				break;
			}
		}
		if (!flag)
		{
			MessageBox.Show("未在配置文件中找到对应的节点", "异常", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			return;
		}
		xmlDocument.Save(Common.CONFIG_FILE_FULL_PATH);
		tvStations.Nodes.Remove(tvStations.SelectedNode);
	}

	/// <summary>
	/// 弹出编辑站点窗体
	/// </summary>
	private void ShowEditStationForm()
	{
		if (tvStations.SelectedNode != null)
		{
			Station station = (Station)tvStations.SelectedNode.Tag;
			StationEditForm stationEditForm = new StationEditForm(EnumFormPurpose.Edit, station)
			{
				Icon = base.Icon,
				StartPosition = FormStartPosition.CenterParent
			};
			stationEditForm.SaveStation += StationEditForm_EditStation;
			stationEditForm.ShowDialog();
		}
	}

	/// <summary>
	/// 编辑站点-保存
	/// </summary>
	/// <param name="station"></param>
	private void StationEditForm_EditStation(Station station)
	{
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(Common.CONFIG_FILE_FULL_PATH);
		XmlElement documentElement = xmlDocument.DocumentElement;
		XmlElement xmlElement = (XmlElement)documentElement.SelectSingleNode("Stations");
		bool flag = false;
		foreach (XmlNode item in xmlElement.SelectNodes("Station"))
		{
			Guid guid = new Guid(item.Attributes["Id"].Value);
			if (guid == station.Id)
			{
				item.Attributes["Name"].Value = station.Name;
				item.SelectSingleNode("Desc").InnerText = station.Desc;
				item.SelectSingleNode("DefaultDeviceId").InnerText = station.DefaultDeviceId.ToString();
				flag = true;
				break;
			}
		}
		if (!flag)
		{
			MessageBox.Show("未在配置文件中找到对应的节点", "异常", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			return;
		}
		xmlDocument.Save(Common.CONFIG_FILE_FULL_PATH);
		TreeNode treeNode = tvStations.Nodes.Find(station.Id.ToString(), searchAllChildren: true)[0];
		treeNode.Text = station.Name;
		treeNode.ToolTipText = station.Desc;
		treeNode.Tag = station;
		tvStations.SelectedNode = treeNode;
	}

	/// <summary>
	/// 上移
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnUp_Click(object sender, EventArgs e)
	{
		if (tvStations.SelectedNode == null)
		{
			return;
		}
		TreeNode selectedNode = tvStations.SelectedNode;
		int index = tvStations.SelectedNode.Index;
		if (index == 0)
		{
			return;
		}
		Station station = (Station)selectedNode.Tag;
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(Common.CONFIG_FILE_FULL_PATH);
		XmlElement documentElement = xmlDocument.DocumentElement;
		XmlElement xmlElement = (XmlElement)documentElement.SelectSingleNode("Stations");
		bool flag = false;
		foreach (XmlNode item in xmlElement.SelectNodes("Station"))
		{
			Guid guid = new Guid(item.Attributes["Id"].Value);
			if (guid == station.Id)
			{
				flag = true;
				xmlElement.InsertBefore(item, xmlElement.ChildNodes[index - 1]);
				break;
			}
		}
		if (!flag)
		{
			MessageBox.Show("未在配置文件中找到对应的节点", "异常", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			return;
		}
		xmlDocument.Save(Common.CONFIG_FILE_FULL_PATH);
		tvStations.Nodes.Remove(selectedNode);
		tvStations.Nodes.Insert(index - 1, selectedNode);
		tvStations.SelectedNode = selectedNode;
	}

	/// <summary>
	/// 下移
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnDown_Click(object sender, EventArgs e)
	{
		if (tvStations.SelectedNode == null)
		{
			return;
		}
		TreeNode selectedNode = tvStations.SelectedNode;
		int index = tvStations.SelectedNode.Index;
		if (index == tvStations.Nodes.Count - 1)
		{
			return;
		}
		Station station = (Station)selectedNode.Tag;
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(Common.CONFIG_FILE_FULL_PATH);
		XmlElement documentElement = xmlDocument.DocumentElement;
		XmlElement xmlElement = (XmlElement)documentElement.SelectSingleNode("Stations");
		bool flag = false;
		foreach (XmlNode item in xmlElement.SelectNodes("Station"))
		{
			Guid guid = new Guid(item.Attributes["Id"].Value);
			if (guid == station.Id)
			{
				flag = true;
				xmlElement.InsertAfter(item, xmlElement.ChildNodes[index + 1]);
				break;
			}
		}
		if (!flag)
		{
			MessageBox.Show("未在配置文件中找到对应的节点", "异常", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			return;
		}
		xmlDocument.Save(Common.CONFIG_FILE_FULL_PATH);
		tvStations.Nodes.Remove(selectedNode);
		tvStations.Nodes.Insert(index + 1, selectedNode);
		tvStations.SelectedNode = selectedNode;
	}

	/// <summary>
	/// 仅在右键单击Node时展开菜单
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void tvStations_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
	{
		Point pt = new Point(e.X, e.Y);
		TreeNode nodeAt = tvStations.GetNodeAt(pt);
		if (nodeAt != null && e.Button == MouseButtons.Right)
		{
			tvStations.SelectedNode = nodeAt;
			tvStations.SelectedNode.ContextMenuStrip = cmsStation;
		}
	}

	/// <summary>
	/// 左键双击时-编辑站点
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void tvStations_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
	{
		if (e.Button == MouseButtons.Left)
		{
			ShowEditStationForm();
		}
	}

	/// <summary>
	/// 按下Del键时-删除站点
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void tvStations_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Delete)
		{
			DeleteStation();
		}
	}

	/// <summary>
	/// Edit Station
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void tsmiEdit_Click(object sender, EventArgs e)
	{
		ShowEditStationForm();
	}

	/// <summary>
	/// Delete Station
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void tsmiDel_Click(object sender, EventArgs e)
	{
		DeleteStation();
	}

	/// <summary>
	/// 实时数据监控
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void tsmiMonitor_Click(object sender, EventArgs e)
	{
		ShowMonitorForm((Station)tvStations.SelectedNode.Tag);
	}

	/// <summary>
	/// 设置采集项和任务
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void tsmiDAQSetting_Click(object sender, EventArgs e)
	{
		Station station = (Station)tvStations.SelectedNode.Tag;
		if (Common.DAQMGR_FORM == null)
		{
			Common.DAQMGR_FORM = new DAQMgrForm(station.Id)
			{
				Icon = base.Icon,
				StartPosition = FormStartPosition.Manual
			};
			Common.DAQMGR_FORM.Disposed += delegate
			{
				Common.DAQMGR_FORM = null;
			};
			Rectangle workingArea = Screen.GetWorkingArea(this);
			Common.DAQMGR_FORM.Location = new Point(base.Location.X + (base.Width - Common.DAQMGR_FORM.Width) / 2, base.Location.Y + (base.Height - Common.DAQMGR_FORM.Height) / 2);
			Common.DAQMGR_FORM.Show();
		}
		else
		{
			if (!Common.DAQMGR_FORM.Visible)
			{
				Common.DAQMGR_FORM.Visible = true;
			}
			if (Common.DAQMGR_FORM.WindowState == FormWindowState.Minimized)
			{
				Common.DAQMGR_FORM.WindowState = FormWindowState.Normal;
			}
			Common.DAQMGR_FORM.Activate();
		}
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WIT.DAQ.Core.Forms.StationMgrForm));
		this.cmsStation = new System.Windows.Forms.ContextMenuStrip(this.components);
		this.tsmiEdit = new System.Windows.Forms.ToolStripMenuItem();
		this.tsmiMonitor = new System.Windows.Forms.ToolStripMenuItem();
		this.tsmiDAQSetting = new System.Windows.Forms.ToolStripMenuItem();
		this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
		this.tsmiDel = new System.Windows.Forms.ToolStripMenuItem();
		this.tvStations = new System.Windows.Forms.TreeView();
		this.btnDel = new System.Windows.Forms.Button();
		this.btnAdd = new System.Windows.Forms.Button();
		this.btnUp = new System.Windows.Forms.Button();
		this.btnDown = new System.Windows.Forms.Button();
		this.cmsStation.SuspendLayout();
		base.SuspendLayout();
		this.cmsStation.Items.AddRange(new System.Windows.Forms.ToolStripItem[5] { this.tsmiEdit, this.tsmiMonitor, this.tsmiDAQSetting, this.toolStripSeparator1, this.tsmiDel });
		this.cmsStation.Name = "cmsDevices";
		this.cmsStation.Size = new System.Drawing.Size(171, 98);
		this.tsmiEdit.Image = global::WIT_DAQ_Core_Properties_Resour.edit_black;
		this.tsmiEdit.Name = "tsmiEdit";
		this.tsmiEdit.Size = new System.Drawing.Size(170, 22);
		this.tsmiEdit.Text = "编辑站点";
		this.tsmiEdit.Click += new System.EventHandler(tsmiEdit_Click);
		this.tsmiMonitor.Name = "tsmiMonitor";
		this.tsmiMonitor.Size = new System.Drawing.Size(170, 22);
		this.tsmiMonitor.Text = "实时数据监控";
		this.tsmiMonitor.Click += new System.EventHandler(tsmiMonitor_Click);
		this.tsmiDAQSetting.Name = "tsmiDAQSetting";
		this.tsmiDAQSetting.Size = new System.Drawing.Size(170, 22);
		this.tsmiDAQSetting.Text = "设置采集项&&任务";
		this.tsmiDAQSetting.Click += new System.EventHandler(tsmiDAQSetting_Click);
		this.toolStripSeparator1.Name = "toolStripSeparator1";
		this.toolStripSeparator1.Size = new System.Drawing.Size(167, 6);
		this.tsmiDel.Image = global::WIT_DAQ_Core_Properties_Resour.del_black;
		this.tsmiDel.Name = "tsmiDel";
		this.tsmiDel.Size = new System.Drawing.Size(170, 22);
		this.tsmiDel.Text = "删除站点";
		this.tsmiDel.Click += new System.EventHandler(tsmiDel_Click);
		this.tvStations.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.tvStations.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
		this.tvStations.ItemHeight = 15;
		this.tvStations.Location = new System.Drawing.Point(4, 7);
		this.tvStations.Name = "tvStations";
		this.tvStations.ShowNodeToolTips = true;
		this.tvStations.Size = new System.Drawing.Size(340, 501);
		this.tvStations.TabIndex = 6;
		this.tvStations.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(tvStations_NodeMouseClick);
		this.tvStations.NodeMouseDoubleClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(tvStations_NodeMouseDoubleClick);
		this.tvStations.KeyDown += new System.Windows.Forms.KeyEventHandler(tvStations_KeyDown);
		this.btnDel.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnDel.BackgroundImage = global::WIT_DAQ_Core_Properties_Resour.del_black;
		this.btnDel.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
		this.btnDel.Location = new System.Drawing.Point(349, 43);
		this.btnDel.Name = "btnDel";
		this.btnDel.Size = new System.Drawing.Size(30, 30);
		this.btnDel.TabIndex = 1;
		this.btnDel.UseVisualStyleBackColor = true;
		this.btnDel.Click += new System.EventHandler(btnDel_Click);
		this.btnAdd.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnAdd.BackgroundImage = (System.Drawing.Image)resources.GetObject("btnAdd.BackgroundImage");
		this.btnAdd.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
		this.btnAdd.Location = new System.Drawing.Point(349, 7);
		this.btnAdd.Name = "btnAdd";
		this.btnAdd.Size = new System.Drawing.Size(30, 30);
		this.btnAdd.TabIndex = 0;
		this.btnAdd.UseVisualStyleBackColor = true;
		this.btnAdd.Click += new System.EventHandler(btnAdd_Click);
		this.btnUp.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnUp.BackgroundImage = (System.Drawing.Image)resources.GetObject("btnUp.BackgroundImage");
		this.btnUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
		this.btnUp.Location = new System.Drawing.Point(349, 115);
		this.btnUp.Name = "btnUp";
		this.btnUp.Size = new System.Drawing.Size(30, 30);
		this.btnUp.TabIndex = 2;
		this.btnUp.UseVisualStyleBackColor = true;
		this.btnUp.Click += new System.EventHandler(btnUp_Click);
		this.btnDown.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnDown.BackgroundImage = (System.Drawing.Image)resources.GetObject("btnDown.BackgroundImage");
		this.btnDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
		this.btnDown.Location = new System.Drawing.Point(349, 151);
		this.btnDown.Name = "btnDown";
		this.btnDown.Size = new System.Drawing.Size(30, 30);
		this.btnDown.TabIndex = 3;
		this.btnDown.UseVisualStyleBackColor = true;
		this.btnDown.Click += new System.EventHandler(btnDown_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(384, 511);
		base.Controls.Add(this.btnUp);
		base.Controls.Add(this.btnDown);
		base.Controls.Add(this.btnDel);
		base.Controls.Add(this.btnAdd);
		base.Controls.Add(this.tvStations);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.Name = "StationMgrForm";
		this.Text = "站点列表";
		base.Load += new System.EventHandler(StationForm_Load);
		base.Shown += new System.EventHandler(StationMgrForm_Shown);
		this.cmsStation.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
